package com.yupi.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yupi.springbootinit.constant.UserConstant;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.model.entity.User;
import com.yupi.springbootinit.model.vo.UserVO;
import com.yupi.springbootinit.service.UserService;
import com.yupi.springbootinit.service.WxminiAppService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.yupi.springbootinit.constant.UserConstant.USER_LOGIN_STATE;

/**
 * @Author xingyu Xue
 * @Date 2024/4/13 21:55
 */
@Service
@Slf4j
public class WxminiAppServiceImpl implements WxminiAppService {
    @Resource
    private UserService userService;


    /**
     * 盐值，混淆密码
     */
    public static final String SALT = "yupi";
    /**
     * 登录：
     * 1、每次登录时查询表中有没有用户 有用户，则返回用户信息
     * 2、第一次登录，直接注册进user表中，
     * 2、
     * @param openid
     * @return
     */
    @Override
    public UserVO wxlogin(String openid, HttpServletRequest request) {
        //先查询是否已经登录过，数据库里有没有
        User isUser = userService.getOne(new QueryWrapper<User>()
                .eq("openid", openid));
        //数据库内有，脱敏用户数据返回
        if (isUser!=null){
            return userService.getUserVO(isUser);
        }


        //到这里，就是数据库内，没有用户信息，那么这次就把它注册进去
        User user = new User();
        user.setUserProfile("小程序端登录用户");
        user.setOpenId(openid);
        user.setUserRole(UserConstant.DEFAULT_ROLE);
        userService.save(user);
        //设置session
        User sessionUser = userService.getBaseMapper().selectOne(new QueryWrapper<User>().eq("openid", openid));
        request.getSession().setAttribute(USER_LOGIN_STATE, sessionUser);

        User Result = userService.getOne(new QueryWrapper<User>().eq("openid", openid));
        UserVO userVO = userService.getUserVO(Result);
        return userVO;

    }

    @Override
    public User register(String username, String phonenumber, String password, String openid,HttpServletRequest request) {

        //加密密码
        String encryptPassword = DigestUtils.md5DigestAsHex((SALT + password).getBytes());


        //注册到数据库内
        User user = new User();
        user.setUserAccount(phonenumber);
        user.setUserPassword(encryptPassword);
        user.setOpenId(openid);
        user.setPhoneNumber(phonenumber);
        user.setMemberShip(phonenumber);
        user.setUserName(username);
        user.setUserRole(UserConstant.DEFAULT_ROLE);
        //注册后。也应更新session内的数据
        request.getSession().removeAttribute(USER_LOGIN_STATE);
        User sessionUser = userService.getBaseMapper().selectOne(new QueryWrapper<User>().eq("openid", openid));
        request.getSession().setAttribute(USER_LOGIN_STATE, sessionUser);



        boolean save = userService.save(user);

        if (save){
            return user;
        }
        throw  new BusinessException(500,"注册失败！请重试！");


    }
}
